package com.zzy.mccjt.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListUtil {

    public static final int DEFAULT_LIMIT = 500;

    public static <T> List<List<T>> split(List<T> list) {
        return split(list, DEFAULT_LIMIT);
    }

    public static <T> List<List<T>> split(List<T> list, int size) {
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }

        final int listSize = list.size();
        final List<List<T>> result = new ArrayList<>(listSize / size + 1);
        int offset = 0;
        for (int toIdx = size; toIdx <= listSize; offset = toIdx, toIdx += size) {
            result.add(list.subList(offset, toIdx));
        }
        if (offset < listSize) {
            result.add(list.subList(offset, listSize));
        }
        return result;
    }

    public static <T> List<T> convertJson(List<String> values, Class<T> clazz) {
        if (values == null || values.isEmpty()) {
            return Collections.emptyList();
        }
        List<T> result = new ArrayList<>();
        for (String value : values) {
            if (!StringUtils.isEmpty(value)) {
                result.add(JSON.parseObject(value, clazz));
            }
        }
        return result;
    }

}
